/**
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 */

import { createModel } from "@rematch/core";
import { IRootState } from "../../store";
import {
  getUserList,
  getUserById,
  saveUser,
  modifyUser,
  saveRole,
  bindRole,
  deleteRole,
  login
} from "../../pages/User/components/userManage/UserAPI";
import { message } from "antd";
import { homePage, outTime } from "src/config/base";
import Cookies from "js-cookie";

export interface IUser {
  id: string;
  account: string;
  password: string;
  name: string;
  status: string;
  createTime: string;
  modifyTime: string;
  supervise: number;
}

export interface IRole {
  id: string;
  userId: string;
  path: string;
  key: string;
}

interface IState {
  userList: IUser[];
  userById: IUser | null;
  roleList: IRole[];
  pageTotal: number;
}

const initalState: IState = {
  userList: [],
  userById: null,
  roleList: [],
  pageTotal: 0
};

export interface ITagList {
  id: string;
  name: string;
}

export const user = createModel({
  state: initalState,
  reducers: {
    setUserList: (state: IState, payload): IState => {
      return { ...state, userList: payload };
    },
    setUserById: (state: IState, payload): IState => {
      return { ...state, userById: payload };
    },
    setRoleList: (state: IState, payload): IState => {
      return { ...state, roleList: payload };
    }
  },
  effects: dispatch => ({
    async getUserList(payload: any, rootState: IRootState) {
      const res = await getUserList(payload);
      if (res.status !== 200) {
        return;
      }
      initalState.pageTotal = res.data.totalElements;
      await dispatch.user.setUserList(res.data.content);
    },
    async getUserById(id: string, rootState: IRootState) {
      const res = await getUserById(id);
      if (res.status !== 200) {
        return;
      }
      await dispatch.user.setUserById(res.data);
    },
    async saveUser(payload: any, rootState: IRootState) {
      const res = await saveUser(payload);
      if (res.status !== 200) {
        message.error(res.message);
        return;
      }
      dispatch.user.getUserList({
        size: 10,
        name: ""
      });
    },
    async modifyUser(payload: any, rootState: IRootState) {
      const res = await modifyUser(payload);
      if (res.status !== 200) {
        message.error(res.message);
        return;
      }
      dispatch.user.getUserList({
        size: 10,
        name: ""
      });
    },
    async saveRole(payload: any, rootState: IRootState) {
      const res = await saveRole(payload);
      if (res.status !== 200) {
        return;
      }
      message.info("操作成功!");
    },
    async bindRole(payload: any, rootState: IRootState) {
      const res = await bindRole(payload);
      if (res.status !== 200) {
        return;
      }
      Cookies.set("user_role", res.data);
      await dispatch.user.setRoleList(res.data);
    },
    async deleteRole(userId: string, rootState: IRootState) {
      const res = await deleteRole(userId);
      if (res.status !== 200) {
        return;
      }
    },
    async login(payload: any, rootState: IRootState) {
      const res = await login(payload);
      if (res.status !== 200 || res.data.data === "") {
        message.warning("账号密码错误");
        return;
      }
      await dispatch.user.bindRole(res.data.id);
      const exp = new Date();
      const time = exp.getTime() + outTime * 60 * 1000;
      console.log()
      Cookies.set("user", res.data);
      Cookies.set("outTime", String(time));

      location.href = location.origin + "#" + homePage;
    }
  })
});
